package com.mtsmda.bookjava7.chapter11.generics;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import com.mtsmda.bookjava7.chapter11.generics.animals.Animal;
import com.mtsmda.bookjava7.chapter11.generics.animals.AnimalDoctor;
import com.mtsmda.bookjava7.chapter11.generics.animals.AziyaDog;
import com.mtsmda.bookjava7.chapter11.generics.animals.Bird;
import com.mtsmda.bookjava7.chapter11.generics.animals.Cat;
import com.mtsmda.bookjava7.chapter11.generics.animals.Dog;
import com.mtsmda.bookjava7.chapter11.generics.animals.JapanAziyaDog;

public class SimpleGenericEx {

	public static void main(String[] args) {
		List list = new ArrayList();
		list.add(new String("string"));
		list.add(new Object());

		for (Object object : list) {
			System.out.println(object.getClass().getCanonicalName());
		}

		System.out.println(list.get(0));

		System.out.println("-----");

		List<Number> numbers = new ArrayList<>();
		numbers.add(new Integer(15));
		numbers.add(new Double(15.96));

		for (Number number : numbers) {
			System.out.println(number.getClass().getCanonicalName());
		}

		System.out.println("-----");

		List<Animal> animals = new ArrayList<>();
		animals.add(new Dog());
		animals.add(new Cat());
		animals.add(new Dog());
		animals.add(new Bird());

		for (Animal animal : animals) {
			animal.isAnimal();
		}

		System.out.println("-----");

		AnimalDoctor.animalDoctor(animals);

		System.out.println("-----");
		List<Dog> dogs = new ArrayList<>();
		dogs.add(new Dog());
		dogs.add(new Dog());
		dogs.add(new Dog());
		dogs.add(new Dog());
		// AnimalDoctor.animalDoctor(dogs);
		AnimalDoctor.animalDoctor2(animals);
		AnimalDoctor.animalDoctor2(dogs);
		AnimalDoctor.animalDoctor2(new ArrayList<Cat>());

		System.out.println("-----");

		List<? extends Serializable> serializables = new ArrayList<>();
		serializables.add(null);
		// serializables.add(new Dog());
		List<Double> doubles = new ArrayList<>();
		new AziyaDog().isAnimal();

		System.out.println("\n\n\n\n\n-----extends-----");
		List<Animal> animalsDog = new ArrayList<>();
		animalsDog.add(new Dog());
		AnimalDoctor.animal(animalsDog);

		List<Dog> animalsDog2 = new ArrayList<>();
		animalsDog2.add(new Dog());
		AnimalDoctor.animal(animalsDog2);

		List<Cat> animalsCat = new ArrayList<>();
		animalsCat.add(new Cat());
		AnimalDoctor.animal(animalsCat);

		List<Bird> animalsBird = new ArrayList<>();
		animalsBird.add(new Bird());
		AnimalDoctor.animal(animalsBird);

		List<AziyaDog> animalsAziyaDog = new ArrayList<>();
		animalsAziyaDog.add(new AziyaDog());
		AnimalDoctor.animal(animalsAziyaDog);

		// AnimalDoctor.dog(animalsDog);
		AnimalDoctor.dog(animalsDog2);
		// AnimalDoctor.dog(animalsCat);
		// AnimalDoctor.dog(animalsBird);
		AnimalDoctor.dog(animalsAziyaDog);

		System.out.println("\nyyyyyy");

		AnimalDoctor.dog2(animalsDog);
		AnimalDoctor.dog2(animalsDog2);
		// AnimalDoctor.dog2(animalsCat);
		// AnimalDoctor.dog2(animalsBird);
		// AnimalDoctor.dog2(animalsAziyaDog);

		System.out.println("\nyyyyyy");

		AnimalDoctor.japanDog(animalsDog);
		AnimalDoctor.japanDog(animalsDog2);
		// AnimalDoctor.japanDog(animalsCat);
		// AnimalDoctor.japanDog(animalsBird);
		AnimalDoctor.japanDog(animalsAziyaDog);

		List<AziyaDog> animalsJapanAziyaDog = new ArrayList<>();
		animalsJapanAziyaDog.add(new JapanAziyaDog());
		AnimalDoctor.japanDog(animalsJapanAziyaDog);

	}

	void average(List<? extends Number> numbers) {
		Number count = null;
		boolean isFirstTime = true;
		for (Number number : numbers) {
			if (number instanceof Integer) {
				if (isFirstTime) {
					count = new Integer(0);
					isFirstTime = false;
				}
				count = count.intValue() + number.intValue();
			}

			if (number instanceof Double) {
				if (isFirstTime) {
					count = new Double(0);
					isFirstTime = false;
				}
				count = count.doubleValue() + number.doubleValue();
			}

			if (number instanceof Long) {
				if (isFirstTime) {
					count = new Long(0);
					isFirstTime = false;
				}
				count = count.longValue() + number.longValue();
			}
		}

	}

	/*
	 * void addToList(List<? extends Number> list, Number... numbers) { if
	 * (numbers.length != 0) { for (Number number : numbers) {
	 * list.add((number); } } }
	 */
}